package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Conference;
import com.ruoyi.system.service.IConferenceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 会议Controller
 * 
 * @author ruoyi
 * @date 2023-12-20
 */
@Controller
@RequestMapping("/system/conference")
public class ConferenceController extends BaseController
{
    private String prefix = "system/conference";

    @Autowired
    private IConferenceService conferenceService;

    @RequiresPermissions("system:conference:view")
    @GetMapping()
    public String conference()
    {
        return prefix + "/conference";
    }

    /**
     * 查询会议列表
     */
    @RequiresPermissions("system:conference:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Conference conference)
    {
        startPage();
        List<Conference> list = conferenceService.selectConferenceList(conference);
        return getDataTable(list);
    }

    /**
     * 导出会议列表
     */
    @RequiresPermissions("system:conference:export")
    @Log(title = "会议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Conference conference)
    {
        List<Conference> list = conferenceService.selectConferenceList(conference);
        ExcelUtil<Conference> util = new ExcelUtil<Conference>(Conference.class);
        return util.exportExcel(list, "会议数据");
    }

    /**
     * 新增会议
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存会议
     */
    @RequiresPermissions("system:conference:add")
    @Log(title = "会议", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Conference conference)
    {
        conference.setConferenceState("0");
        return toAjax(conferenceService.insertConference(conference));
    }

    /**
     * 修改会议
     */
    @RequiresPermissions("system:conference:edit")
    @GetMapping("/edit/{conferenceId}")
    public String edit(@PathVariable("conferenceId") Long conferenceId, ModelMap mmap)
    {
        Conference conference = conferenceService.selectConferenceByConferenceId(conferenceId);
        mmap.put("conference", conference);
        return prefix + "/edit";
    }

    /**
     * 修改保存会议
     */
    @RequiresPermissions("system:conference:edit")
    @Log(title = "会议", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Conference conference)
    {
        return toAjax(conferenceService.updateConference(conference));
    }

    /**
     * 删除会议
     */
    @RequiresPermissions("system:conference:remove")
    @Log(title = "会议", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(conferenceService.deleteConferenceByConferenceIds(ids));
    }

    @ResponseBody
    @GetMapping("/startAMeeting/{conferenceId}")
    public  AjaxResult startAMeeting(@PathVariable("conferenceId") Long conferenceId) {

            Conference conference = new Conference();
            conference.setConferenceId(conferenceId);
            conference.setBeginTime(new Date());

            conference.setConferenceState("1");
            return toAjax(conferenceService.updateConference(conference));
        }
    @ResponseBody
    @GetMapping("/endAMeeting/{conferenceId}")
    public  AjaxResult endAMeeting(@PathVariable("conferenceId") Long conferenceId)
    {
        Conference confer = conferenceService.selectConferenceByConferenceId(conferenceId);
        if(Objects.equals(confer.getConferenceState(),"0")){
            return error("会议还没开始，不能结束");
        }else {
            Conference conference = new Conference();
            conference.setConferenceId(conferenceId);
            conference.setEndTime(new Date());
            conference.setConferenceState("2");
            return toAjax(conferenceService.updateConference(conference));
        }

    }
}


